#!/usr/bin/env python3
#
# Project Wok
#
# Copyright IBM Corp, 2015-2017
#
# Code derived from Project Kimchi
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301  USA

import os
import sys
sys.path.insert(1, '@pythondir@')

from optparse import OptionParser

import wok.server
import wok.config as config


if not config.paths.installed:
    sys.path.append(config.paths.prefix)

ACCESS_LOG = "wok-access.log"
ERROR_LOG = "wok-error.log"


def main(options):
    # Script must run as root or with sudo.
    if not os.geteuid() == 0:
        sys.exit("\nMust be root to run this script. Exiting ...\n")

    proxy_port = config.config.get("server", "proxy_port")
    cherrypy_port = config.config.get("server", "cherrypy_port")
    websockets_port = config.config.get("server", "websockets_port")
    session_timeout = config.config.get("server", "session_timeout")
    runningEnv = config.config.get("server", "environment")
    federation = config.config.get("server", "federation")
    server_root = config.config.get("server", "server_root")
    logDir = config.config.get("logging", "log_dir")
    logLevel = config.config.get("logging", "log_level")

    parser = OptionParser()
    parser.add_option('--proxy_port', type="int", default=proxy_port,
                      help="Port to enable SSL (default %s)" % proxy_port)
    parser.add_option('--cherrypy_port', type="int", default=cherrypy_port,
                      help="Cherrypy server port (default %s)" % cherrypy_port)
    parser.add_option('--websockets_port', type="int", default=websockets_port,
                      help="Websockets port to listen on (default %s)" %
                            websockets_port)
    parser.add_option('--session_timeout', type="int", default=session_timeout,
                      help="Number of minutes that a session can remain idle "
                           "before the server terminates it automatically. "
                           "(default %s)" % session_timeout)
    parser.add_option('--log_level', default=logLevel,
                      help="Logging level")
    parser.add_option('--log_dir', default=logDir, help="Log directory")
    parser.add_option('--environment', default=runningEnv,
                      help="Running environment of wok server")
    parser.add_option('--server_root', type="string", default=server_root,
                      help="Relative path to server")
    parser.add_option('--federation', default=federation,
                      help="Register and discover Wok peers in the same "
                           "network using openSLP. Check README-federation for"
                           " more details.")
    parser.add_option('--test', action='store_true',
                      help="Run server in mock model")
    (options, args) = parser.parse_args()

    setattr(options, 'access_log', os.path.join(options.log_dir, ACCESS_LOG))
    setattr(options, 'error_log', os.path.join(options.log_dir, ERROR_LOG))

    # Add non-option arguments
    setattr(options, 'max_body_size',
            config.config.get('server', 'max_body_size'))

    wok.server.main(options)

if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))
